import { defineStore } from "pinia";
import axios from "axios";
import { ref } from 'vue';

export const useHttpStoreDanger = defineStore('httpStoreDanger', () => {
    // 状态
    const dId = ref(null); // 接收id
    const dangerChart01 = ref({}) // 未修复告警
    const dangerdata = ref([]) // 告警异常信息
    const worknote = ref([]) // 告警处理报告
    const dangerChart02 = ref({}) // 告警处理数据
    const isLoading = ref(false); // 加载状态
    const error = ref(null); // 错误信息

    const clearDevices = () => {
        dId.value = null;
        dangerChart01.value = {};
        dangerdata.value = [];
        worknote.value = [];
        dangerChart02.value = {};
    }

    // 获取
    const fetchDangerData = async (time) => {
        isLoading.value = true;
        error.value = null;
        try {
            const response = await axios.get(`http://192.168.31.96:5000/dangerCharts/${time}`);
            console.log("aaaaaaaaa",response);
            
            dId.value = response.data.id;
            dangerChart01.value = response.data.dangerChart01;
            dangerdata.value = response.data.dangerdata;
            worknote.value = response.data.worknote;
            dangerChart02.value = response.data.dangerChart02;
        } catch (err) {
            error.value = "获取dangerCharts数据失败";
            console.error(err)
        } finally {
            if (worknote.value != [] || dangerChart02.value != {}) {
                isLoading.value = true;
            }
            isLoading.value = false;
        }
    }
    return {
        // 状态
        dId,
        dangerChart01,
        dangerdata,
        worknote,
        dangerChart02,
        isLoading,
        error,

        // 方法
        clearDevices,
        fetchDangerData
    }
});

export const useHttpStoreRedanger = defineStore('httpStoreRedanger', () => {
    // 状态
    const dId = ref(null); // 预警id
    const chartdata = ref([]); // 预警图表
    const startdata = ref(""); // 开始时间
    const enddata = ref(""); // 结束时间
    const mindata = ref(null); // 最小范围
    const maxdata = ref(null); // 最大范围
    const redangerpaper = ref(""); // 预警分析报告 markdown
    const isLoading = ref(false)
    const clearDevices = () => {
        dId.value = null;
        chartdata.value = [];
        startdata.value = "";
        enddata.value = "";
        mindata.value = null;
        maxdata.value = null;
        redangerpaper.value = ""
    }

    // 获取
    const fetchRedangerData = async (id) => {
        try {
            const response = await axios.get(`http://192.168.31.96:5000/redangerCharts/${id}`)
            dId.value = response.data.id;
            chartdata.value = response.data.chartdata;
            startdata.value = response.data.startdata;
            enddata.value = response.data.enddata;
            mindata.value = response.data.mindata;
            maxdata.value = response.data.maxdata;
            redangerpaper.value = response.data.redangerpaper;
            console.log("bbbbbbbbb", response.data);
        } catch (err) {
            console.error(err);
        } finally {
            if (!dId.value) {
                isLoading.value = false
            }else {
                isLoading.value = true
            }
        }
    }
    return {
        dId,
        chartdata,
        startdata,
        enddata,
        mindata,
        maxdata,
        redangerpaper,
        clearDevices,
        fetchRedangerData
    }

})